package com.letv.core.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.letv.core.bean.LiveBeanLeChannel;
import com.letv.core.bean.LiveBeanLeChannelList;
import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.LetvConstant;
import com.letv.core.utils.LetvTools;

public class ChannelListHandler {	
	private Context mContext;
	public ChannelListHandler(Context mContext){
		this.mContext=mContext;
	}
	/**
	 * 更新频道表 
	 */
	public synchronized boolean updateByChannelId(LiveBeanLeChannel liveLunboWeishi, String type){
		try {
			if(liveLunboWeishi!=null){
				ContentValues values=new ContentValues();
				values.put(DatabaseConstant.ChannelListTrace.Field.CHANNELID, liveLunboWeishi.getChannelId());
				values.put(DatabaseConstant.ChannelListTrace.Field.NUMERICKEYS, liveLunboWeishi.getNumericKeys());
				values.put(DatabaseConstant.ChannelListTrace.Field.NAME, liveLunboWeishi.getChannelName());
				values.put(DatabaseConstant.ChannelListTrace.Field.ENAME, liveLunboWeishi.getChannelEname());
				values.put(DatabaseConstant.ChannelListTrace.Field.HASSAVE,liveLunboWeishi.getSaveFlag());
				mContext.getContentResolver().update(LetvContentProvider.URI_CHANNELLISTTRACE, values,
						DatabaseConstant.ChannelListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?", new String[] {liveLunboWeishi.getChannelId()+"", type });
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}	
	} 
	/**
	 * 更新收藏频道状态
	 *  
	 * 
	 */
	public synchronized boolean updateChannelStatus(LiveBeanLeChannel saveChannel,String channelStatus, String type){
		ContentValues values = new ContentValues();
		values.put(DatabaseConstant.ChannelListTrace.Field.CHANNELSTATUS, channelStatus);
        values.put(DatabaseConstant.ChannelListTrace.Field.CHANNELID, saveChannel.getChannelId());
		values.put(DatabaseConstant.ChannelListTrace.Field.NUMERICKEYS, saveChannel.getNumericKeys());
		values.put(DatabaseConstant.ChannelListTrace.Field.NAME, saveChannel.getChannelName());
		values.put(DatabaseConstant.ChannelListTrace.Field.ENAME, saveChannel.getChannelEname());
//		values.put(DatabaseConstant.ChannelListTrace.Field.BEGINTIME, saveChannel.getBeginTime());
//		values.put(DatabaseConstant.ChannelListTrace.Field.ENDTIME, saveChannel.getEndTime());
//		values.put(DatabaseConstant.ChannelListTrace.Field.CHANNELCLASS, saveChannel.getChannelClass());
//		values.put(DatabaseConstant.ChannelListTrace.Field.BELONGBRAND, saveChannel.getBelongBrand());
//		values.put(DatabaseConstant.ChannelListTrace.Field.SOURCEID, saveChannel.getSourceId());
//		values.put(DatabaseConstant.ChannelListTrace.Field.IS3D, saveChannel.getIs3D());
//		values.put(DatabaseConstant.ChannelListTrace.Field.IS4K, saveChannel.getIs4K());
//		values.put(DatabaseConstant.ChannelListTrace.Field.CH,saveChannel.getCh());
//		values.put(DatabaseConstant.ChannelListTrace.Field.ORDERNO,saveChannel.getOrderNo());
//		values.put(DatabaseConstant.ChannelListTrace.Field.ISCOMMEND,saveChannel.getIsRecommend());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGO120X90,saveChannel.getLogo120x90());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGOORIGIN,saveChannel.getLogoOrigin());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGO150X200,saveChannel.getLogo150x200());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGO120X160,saveChannel.getLogo120x160());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGO96X128,saveChannel.getLogo96x128());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGO150X150,saveChannel.getLogo150x150());
//		values.put(DatabaseConstant.ChannelListTrace.Field.LOGO30X30,saveChannel.getLogo30x30());
        try {
            return mContext.getContentResolver().update(
                    LetvContentProvider.URI_CHANNELLISTTRACE,
                    values,
                    DatabaseConstant.ChannelListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{saveChannel.getChannelId(), type}) == 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
	}
	
	/**
	 * 更新收藏频道状态
	 *  
	 * 
	 */
	public synchronized boolean delete(LiveBeanLeChannel saveChannel, String type){
        try {
            return mContext.getContentResolver().delete(
                    LetvContentProvider.URI_CHANNELLISTTRACE,
                    DatabaseConstant.ChannelListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{saveChannel.getChannelId(), type}) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
	}
	/**
	 * 更新频道表  使用排序号更新
	 */
	public synchronized boolean updateByNumberkeys(LiveBeanLeChannel liveLunboWeishi,String channelStatus,String saveFlag, String type){
		try {
			if(liveLunboWeishi!=null){
				ContentValues values=new ContentValues();
				values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID, liveLunboWeishi.getChannelId());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS, liveLunboWeishi.getNumericKeys());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.NAME, liveLunboWeishi.getChannelName());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.ENAME, liveLunboWeishi.getChannelEname());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.BEGINTIME, liveLunboWeishi.getBeginTime());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.ENDTIME, liveLunboWeishi.getEndTime());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELCLASS, liveLunboWeishi.getChannelClass());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.BELONGBRAND, liveLunboWeishi.getBelongBrand());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.SOURCEID, liveLunboWeishi.getSourceId());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.IS3D, liveLunboWeishi.getIs3D());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.IS4K, liveLunboWeishi.getIs4K());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.CH,liveLunboWeishi.getCh());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.ORDERNO,liveLunboWeishi.getOrderNo());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.ISCOMMEND,liveLunboWeishi.getIsRecommend());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X90,liveLunboWeishi.getLogo120x90());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGOORIGIN,liveLunboWeishi.getLogoOrigin());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X200,liveLunboWeishi.getLogo150x200());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X160,liveLunboWeishi.getLogo120x160());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO96X128,liveLunboWeishi.getLogo96x128());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X150,liveLunboWeishi.getLogo150x150());
//				values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO30X30,liveLunboWeishi.getLogo30x30());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.ISRECORD, saveFlag);
				values.put(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND, liveLunboWeishi.getCurrentmillisecond());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS, channelStatus);
				mContext.getContentResolver().update(LetvContentProvider.URI_CHANNELLISTTRACE, values,
						DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS + "=? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?", new String[] {liveLunboWeishi.getNumericKeys()+"", type });
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}	
	}
	
	
	
	/**
	 * 得到所有的频道
	 */
	public synchronized LiveBeanLeChannelList getAllChannelList(String type){
		Cursor cursor = null;
		LiveBeanLeChannelList lunboWeishiList = null;
		try {
			cursor=mContext.getContentResolver().query(LetvContentProvider.URI_CHANNELLISTTRACE, null,
					/*DatabaseConstant.ChannelListTrace.Field.CHANNELSTATUS + "= ? and " +*/ DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { /*DatabaseConstant.ChannelListTrace.ChannelStatus.NORMAL + "",*/ type }, null);
			lunboWeishiList=new LiveBeanLeChannelList(); 
			Log.i("wxy", "cursor count:"+cursor.getCount());
			while (cursor.moveToNext()) {
				LiveBeanLeChannel livelunboweishi=new LiveBeanLeChannel();
				livelunboweishi.setChannelId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.CHANNELID)));
				if ("weishi".equals(type)) {
					livelunboweishi.setNumericKeys("");
				}else{
					livelunboweishi.setNumericKeys(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.NUMERICKEYS)));
				}
				
				livelunboweishi.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.NAME)));
				livelunboweishi.setChannelEname(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ENAME)));
//				livelunboweishi.setBeginTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.BEGINTIME)));
//				livelunboweishi.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ENDTIME)));
//				livelunboweishi.setChannelClass(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.CHANNELCLASS)));
//				livelunboweishi.setBelongBrand(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.BELONGBRAND)));
//				livelunboweishi.setSourceId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.SOURCEID)));
//				livelunboweishi.setIs3D(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.IS3D)));
//				livelunboweishi.setIs4K(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.IS4K)));
//				livelunboweishi.setCh(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.CH)));
//				livelunboweishi.setOrderNo(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ORDERNO)));
//				livelunboweishi.setIsRecommend(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ISCOMMEND)));
//				livelunboweishi.setLogo120x90(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO120X90)));
//				livelunboweishi.setLogoOrigin(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGOORIGIN)));
//				livelunboweishi.setLogo150x200(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO150X200)));
//				livelunboweishi.setLogo120x160(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO120X160)));
//				livelunboweishi.setLogo96x128(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO96X128)));
//				livelunboweishi.setLogo150x150(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO150X150)));
//				livelunboweishi.setLogo30x30(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO30X30)));
				livelunboweishi.setSaveFlag(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.HASSAVE)));
				lunboWeishiList.add(livelunboweishi);
			}
		} catch (Exception e) {
			Log.e("wxy", "find data", e);
			e.printStackTrace();
		}finally{
			LetvTools.closeCursor(cursor);
		}
		return lunboWeishiList;
	}
	/**
	 * 获取收藏的Channel
	 */
	public synchronized LiveBeanLeChannelList getSaveChannelList(String type){
		Cursor cursor = null;
		LiveBeanLeChannelList lunboWeishiList = null;
		try {
			cursor = mContext.getContentResolver().query(
					LetvContentProvider.URI_CHANNELLISTTRACE, null,
					DatabaseConstant.ChannelListTrace.Field.HASSAVE + "= ? and "+DatabaseConstant.ChannelListTrace.Field.CHANNELSTATUS+"= ? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { 1 + "",DatabaseConstant.ChannelListTrace.ChannelStatus.NORMAL + "", type }, null);
			lunboWeishiList=new LiveBeanLeChannelList(); 
			while (cursor.moveToNext()) {
				LiveBeanLeChannel livelunboweishi=new LiveBeanLeChannel();
				livelunboweishi.setChannelId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.CHANNELID)));
				livelunboweishi.setNumericKeys(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.NUMERICKEYS)));
				livelunboweishi.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.NAME)));
				livelunboweishi.setChannelEname(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ENAME)));
//				livelunboweishi.setBeginTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.BEGINTIME)));
//				livelunboweishi.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ENDTIME)));
//				livelunboweishi.setChannelClass(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.CHANNELCLASS)));
//				livelunboweishi.setBelongBrand(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.BELONGBRAND)));
//				livelunboweishi.setSourceId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.SOURCEID)));
//				livelunboweishi.setIs3D(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.IS3D)));
//				livelunboweishi.setIs4K(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.IS4K)));
//				livelunboweishi.setCh(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.CH)));
//				livelunboweishi.setOrderNo(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ORDERNO)));
//				livelunboweishi.setIsRecommend(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.ISCOMMEND)));
//				livelunboweishi.setLogo120x90(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO120X90)));
//				livelunboweishi.setLogoOrigin(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGOORIGIN)));
//				livelunboweishi.setLogo150x200(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO150X200)));
//				livelunboweishi.setLogo120x160(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO120X160)));
//				livelunboweishi.setLogo96x128(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO96X128)));
//				livelunboweishi.setLogo150x150(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO150X150)));
//				livelunboweishi.setLogo30x30(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.LOGO30X30)));
				livelunboweishi.setSaveFlag(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.HASSAVE)));
				lunboWeishiList.add(livelunboweishi);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			LetvTools.closeCursor(cursor);
		}
		return lunboWeishiList;
	}
	/**
	 * 添加一条记录到频道表中
	 */
	public synchronized boolean addToChannelList(LiveBeanLeChannel liveLunboWeishi, String type){
		Log.i("wxy", "addToChannelList: "+liveLunboWeishi+",type:"+type);
		try {
			if(liveLunboWeishi!=null){
				Log.i("wxy", "isExist(liveLunboWeishi.getChannelId(), type): "+isExist(liveLunboWeishi.getChannelId(), type));
				if(isExist(liveLunboWeishi.getChannelId(), type)){
					return true;
				}
			}
			ContentValues values=new ContentValues();
			values.put(DatabaseConstant.ChannelListTrace.Field.CHANNELID, liveLunboWeishi.getChannelId());
			values.put(DatabaseConstant.ChannelListTrace.Field.NUMERICKEYS,liveLunboWeishi.getNumericKeys());
			values.put(DatabaseConstant.ChannelListTrace.Field.NAME, liveLunboWeishi.getChannelName());
			values.put(DatabaseConstant.ChannelListTrace.Field.ENAME, liveLunboWeishi.getChannelEname());
			values.put(DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE, type);
//			values.put(DatabaseConstant.ChannelListTrace.Field.BEGINTIME, liveLunboWeishi.getBeginTime());
//			values.put(DatabaseConstant.ChannelListTrace.Field.ENDTIME, liveLunboWeishi.getEndTime());
//			values.put(DatabaseConstant.ChannelListTrace.Field.CHANNELCLASS, liveLunboWeishi.getChannelClass());
//			values.put(DatabaseConstant.ChannelListTrace.Field.BELONGBRAND, liveLunboWeishi.getBelongBrand());
//			values.put(DatabaseConstant.ChannelListTrace.Field.SOURCEID, liveLunboWeishi.getSourceId());
//			values.put(DatabaseConstant.ChannelListTrace.Field.IS3D, liveLunboWeishi.getIs3D());
//			values.put(DatabaseConstant.ChannelListTrace.Field.IS4K, liveLunboWeishi.getIs4K());
//			values.put(DatabaseConstant.ChannelListTrace.Field.CH,liveLunboWeishi.getCh());
//			values.put(DatabaseConstant.ChannelListTrace.Field.ORDERNO,liveLunboWeishi.getOrderNo());
//			values.put(DatabaseConstant.ChannelListTrace.Field.ISCOMMEND,liveLunboWeishi.getIsRecommend());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGO120X90,liveLunboWeishi.getLogo120x90());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGOORIGIN,liveLunboWeishi.getLogoOrigin());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGO150X200,liveLunboWeishi.getLogo150x200());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGO120X160,liveLunboWeishi.getLogo120x160());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGO96X128,liveLunboWeishi.getLogo96x128());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGO150X150,liveLunboWeishi.getLogo150x150());
//			values.put(DatabaseConstant.ChannelListTrace.Field.LOGO30X30,liveLunboWeishi.getLogo30x30());
			values.put(DatabaseConstant.ChannelListTrace.Field.HASSAVE,liveLunboWeishi.getSaveFlag());
			mContext.getContentResolver().insert(LetvContentProvider.URI_CHANNELLISTTRACE, values);
			return true;
		} catch (Exception e) {
			Log.e("wxy", "add save error",e);
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 是否存在
	 * @param ename
	 * @return
	 */
	public synchronized boolean isExist(String channelId, String type){
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_CHANNELLISTTRACE, null, DatabaseConstant.ChannelListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?", new String[]{channelId + "", type}, null);
            if (cursor != null && cursor.getCount() > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }
        return false;
    }
	
	
	/**
	 * 根据排序号查找和下线状态查找
	 * @param numberKeys
	 * @return
	 */
	public boolean isExistByNumberKeysAndStatus(String numberKeys, String type){
		 Cursor cursor = null;
	        try {
	            cursor = mContext.getContentResolver().
	            		query(LetvContentProvider.URI_CHANNELLISTTRACE, 
	            											null, 
	            											DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS + "=? and "+DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS+"= ? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
	            											new String[]{numberKeys + "",DatabaseConstant.ChannelHisListTrace.ChannelStatus.DELETE + "", type},
	            											null);
	            if (cursor != null && cursor.getCount() > 0) {
	                return true;
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            LetvTools.closeCursor(cursor);
	        }
	        return false;
	}
	
	
    /**
     * 收藏、取消收藏频道
     * @param channelId
     * @param isCollect 1 收藏；0 取消收藏
     * @return
     */
    public synchronized boolean updateToChannelList(String channelId, int isCollect, String type) {
        ContentValues cv = new ContentValues();
        cv.put(DatabaseConstant.ChannelListTrace.Field.HASSAVE, isCollect);
        try {
            return mContext.getContentResolver().update(
                    LetvContentProvider.URI_CHANNELLISTTRACE,
                    cv,
                    DatabaseConstant.ChannelListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{channelId, type}) == 1;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 是否已经收藏过
     * @param channelId
     * @return
     */
    public synchronized boolean hasCollectChannel(String channelId, String type) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(
                    LetvContentProvider.URI_CHANNELLISTTRACE, null,
                    DatabaseConstant.ChannelListTrace.Field.CHANNELID + "= ? and " + DatabaseConstant.ChannelListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{channelId, type}, null);

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToNext();
                int has = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelListTrace.Field.HASSAVE));
                return (has == 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return false;
    }
}
